package com.xmg.p2p.base.interceptor;

import com.xmg.p2p.base.util.UserContext;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class LoginCheckInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            RequireLogin methodAnnotation = handlerMethod.getMethodAnnotation(RequireLogin.class);
            if (methodAnnotation != null && UserContext.getLogininfo() == null) {
                response.sendRedirect("login.html");
                return false;
            }
        }
        return super.preHandle(request, response, handler);
    }
}
